//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;

namespace Eas.WebCrawler.Test.PlugIns {

    public enum TestDirectory { Source, Target };

    public static class TestDirectoryHelper {
        public static readonly System.IO.DirectoryInfo SourceDir;
        public static readonly System.IO.DirectoryInfo TargetDir;
        private const string TempSourceDirName = "WebCrawlerTestSource";
        private const string TempTargetDirName = "WebCrawlerTestTarget";

        static TestDirectoryHelper() {
            string tempPath = System.IO.Path.GetTempPath();
            SourceDir = new System.IO.DirectoryInfo(System.IO.Path.Combine(tempPath, TempSourceDirName));
            TargetDir = new System.IO.DirectoryInfo(System.IO.Path.Combine(tempPath, TempTargetDirName));
        }

        public static void Setup() {
            TearDown();

            System.IO.Directory.CreateDirectory(SourceDir.FullName);
            System.IO.Directory.CreateDirectory(TargetDir.FullName);

            CopySource("HelloWorld.html");
            CopySource("test.html");
            CopySource("Text.txt");
            CopySource("TextReference.html");
            CopySource("Uris.txt");
            CopySource("Uris2.txt");
            CopySource("Test.aspx");
            CopySource("Test2.htm");
            CopySource("Test3.htm");
            CopySource("Test4.htm");
            CopySource("Test5.htm");
            CopySource("Test6.htm");
            CopySource("Test7.htm");
            CopySource("Test8.htm");
            CopySource("Test9.htm");
            CopySource("Test91.htm");
            CopySource("LinkList.htm");
        }

        public static void TearDown() {
            if (System.IO.Directory.Exists(SourceDir.FullName))
                System.IO.Directory.Delete(SourceDir.FullName, true);
            if (System.IO.Directory.Exists(TargetDir.FullName))
                System.IO.Directory.Delete(TargetDir.FullName, true);
        }

        private static void CopySource(string resourceName) {
            using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Eas.WebCrawler.Test.PlugIns.Resources." + resourceName)) {
                using (System.IO.StreamReader reader = new System.IO.StreamReader(stream)) {
                    using (System.IO.StreamWriter w = Create(resourceName)) {
                        string line = reader.ReadLine();
                        while (line != null) {
                            w.WriteLine(line);
                            line = reader.ReadLine();
                        }
                    }
                }
            }
        }


        public static void CreateEmptyFile(TestDirectory directory, string filename) {
            string filepath = Name(directory, filename);
            using (System.IO.StreamWriter w = System.IO.File.CreateText(filepath)) { }
        }

        public static string Name(TestDirectory directory, string filename) {
            if (directory == TestDirectory.Source)
                return SrcName(filename);
            else
                return TgName(filename);
        }

        private static string SrcName(string filename) { return System.IO.Path.Combine(SourceDir.FullName, filename); }
        private static string TgName(string filename) { return System.IO.Path.Combine(TargetDir.FullName, filename); }

        private static System.IO.StreamWriter Create(string filename) {
            return System.IO.File.CreateText(SrcName(filename));
        }

        private static System.IO.StreamReader Read(string filename) {
            return System.IO.File.OpenText(TgName(filename));
        }

        public static System.IO.StreamReader Read(TestDirectory directory, string filename) {
            return System.IO.File.OpenText(Name(directory, filename));
        }

    }
}
